//MCU心跳包接口
module mcu_state_01(
		input   wire            resetb,
        	input   wire            sclk,

		//写参数接口
	        input	wire		cmd_d_ok,
	        input	wire	[19:0]	cmd_addr,
	        input	wire	[7:0]	cmd_data,	
		
		//读参数接口
		input	wire		mcu_state_active,
		input	wire		mcu_offline_active,
		input	wire	[7:0]	mcu_state_addr,
		output 	wire	[7:0]	mcu_state_data,
		
		input	wire	[7:0]	phy_result,
		input	wire	[15:0]	hub_result,
		input	wire	[7:0]	sdram_result,
		                				
		//状态寄存器等
		input	wire		sd_card_vaild,
//		input	wire		sd_swp,
		output	reg		reboot_en_mcu,
		output	reg		offline_vs_en

		);

//******************************************************************/
//			   信号定义
//******************************************************************/		
//程序版本信息
parameter       main_function   =       8'h53;   //ASCII "S"
parameter       sub_function    =       8'h47;   //ASCII "G"
parameter       main_solution   =       8'd8;    //"8"       
parameter       sub_solution    =       8'd3;    //"03"
parameter       application_type=       8'h84;   //ASCII "T"
parameter       main_version    =       8'd7;    //"07"
parameter       sub_version     =       8'd47;   //X"47"       

parameter	LOGIC_MODE	=	3;	//initial

reg		rec_flag_t;
reg	[2:0]	counter,counter_t,counter_tt;
reg		add_flag,add_flag_t,add_flag_tt;
reg		clr_flag,clr_flag_t;
reg		load_flag,load_flag_t;
reg	[8:0]	adder;
reg		cin;
reg		buf_wea;
reg	[3:0]	buf_waddr;
reg	[7:0]	buf_wdata;
reg	[3:0]	buf_raddr;
wire	[7:0]	buf_rdata;
reg     [7:0]   state_version;

reg	[15:0]	crc_error_sum_1,crc_error_sum_2;
reg	[31:0]	pkt_sum_1,pkt_sum_2;
reg	[7:0]	data_out;
reg		full_crc;
reg		full_pkt;

reg	[2:0]	sd_card_vaild_r;


always @(posedge sclk)
	sd_card_vaild_r<={sd_card_vaild_r[1:0],sd_card_vaild};
	
always @(posedge sclk)
	if(mcu_state_active==1)
	case(mcu_state_addr)
//		'd0:data_out<=sd_card_vaild_r[2];	
		'h10:data_out<=main_function;
		'h11:data_out<=sub_function;
		'h12:data_out<=main_solution;
		'h13:data_out<=sub_solution;
		'h14:data_out<=application_type;
		'h15:data_out<=main_version;
		'h16:data_out<=sub_version;
		
		'h20:data_out<=LOGIC_MODE;
//		'h21:data_out<=phy_result;
//		'h22:data_out<=hub_result[7:0];
//		'h23:data_out<=hub_result[15:8];
//		'h24:data_out<=sdram_result;
		
		default data_out<='d0;
	endcase
//	else if(mcu_offline_active==1)
//	case(mcu_state_addr)
//		'd0:data_out<=offline_vs_en;
//		default data_out<='d0;
//	endcase	
	else
		data_out<=0;

//always	@(posedge sclk or negedge resetb)
//	if (resetb==0)
//		begin
//			offline_vs_en	<=0;
//		end
//	else if(sd_card_vaild_r[2]==0)
//		offline_vs_en<=0;
//	else if (cmd_d_ok==1)
//		case (cmd_addr)
//			20'h10100:	offline_vs_en	<=cmd_data[0];
//		endcase

reg	[7:0]	reboot_mode;

always	@(posedge sclk or negedge resetb)
	if (resetb==0)
		begin
			reboot_mode	<=0;
		end
	else if (cmd_d_ok==1)
		case (cmd_addr)
			20'h10601:	reboot_mode	<=cmd_data;
		endcase

always	@(posedge sclk or negedge resetb)
	if (resetb==0)
		reboot_en_mcu<=0;
	else if((LOGIC_MODE==1 || LOGIC_MODE==2) && reboot_mode==3)
		reboot_en_mcu<=1;
				
assign    mcu_state_data=data_out;


endmodule